package com.example.web.controller;

import com.example.web.utils.ConfigUtils;
import com.sso.client.service.SsoWebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : alex
 * @date : 2020/1/22
 */
@Controller
public class LoginController {
    public static final String TOKEN_COOKIE_KEY = "token";
    public static final Integer COOKIE_AGE_ZERO = 0;
    public static final Integer COOKIE_AGE_NEGATIVE = -1;
    public static final String COOKIE_PATH = "/";
    public static final Integer TOKEN_TTL = 30 * 60 * 1000;
    @Autowired
    SsoWebClient ssoWebClient;
    /**
     * 首页
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request, ModelAndView modelAndView) {
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 退出登录
     *
     * @param request
     * @param response
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/logout")
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        try {
            modelAndView.setViewName("redirect:" + ssoWebClient.logout(request,response));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return modelAndView;
    }
}
